package com.baidu.controller;
/**
 * Controller
 * 体检预约
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.Address;
import com.baidu.pojo.Order;
import com.baidu.pojo.Setmeal;
import com.baidu.service.AddressService;
import com.baidu.service.OrderListService;
import com.baidu.service.SetMealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderListController {
    @Reference
    private OrderListService orderListService;
    @Reference
    private SetMealService setmealService;
    @Reference
    private AddressService addressService;

    /**
     * 分页显示所有预约
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult= orderListService.findPage(queryPageBean);
       return pageResult;
    }

    /**
     * 回显套餐信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Setmeal> setmealList = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     *  回显地址
     * @return
     */
    @RequestMapping("/findAllAddress")
    public Result findAllAddress(){
        try {
            List<Address> addressList=addressService.findAll();
            return new Result(true,MessageConstant.GET_ADDRESS_FAIL,addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ADDRESS_SUCCESS);
        }
    }

    /**
     *  预约
     * @param map
     * @param setmealId
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map map ,Integer[] setmealId){
        if (setmealId!=null && setmealId.length>0){
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            Result result=null;
            try {
                result = orderListService.add(map,setmealId);
                System.out.println(result.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                return result;
            }
            return result;
        }else {
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 预约确定
     * @param id
     * @return
     */
    @RequestMapping("/orderSure")
    public Result orderSure(Integer id){
        Result result=null;
        try {
            result=orderListService.orderSure(id);
        } catch (Exception e) {
            e.printStackTrace();
           return result;
        }
        return result;
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @RequestMapping("/orderCancel")
    public Result orderCancel(Integer id){
        Result result=null;
        try {
            result=orderListService.orderCancel(id);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    /**
     * 删除预约
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete( Integer id){
        Result result=null;
        try {
            result=orderListService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }
}
